#include "progressdlg.h"
#include <QProgressDialog>

ProgressDlg::ProgressDlg(QWidget *parent)
    : QDialog(parent)
{
    QFont font("ZYSongl8030",12);
    setFont(font);
    setWindowTitle(tr("Progress"));
    FileNum =new QLabel;
    FileNum->setText(tr(" 文件数目:"));
    FileNumLineEdit =new QLineEdit;
    FileNumLineEdit->setText(tr("100000"));
    ProgressType =new QLabel;
    ProgressType->setText(tr("显示类型:"));
    comboBox =new QComboBox;
    comboBox->addItem(tr("progressBar"));
    comboBox->addItem(tr("progressDialog"));
    progressBar =new QProgressBar;
    starBtn =new QPushButton ();
    starBtn->setText(tr("开始"));

    mainLayout =new QGridLayout(this);
    mainLayout->addWidget(FileNum,0,0);
    mainLayout->addWidget(FileNumLineEdit,0,1);
    mainLayout->addWidget(ProgressType,1,0);
    mainLayout->addWidget(comboBox,1,1);
    mainLayout->addWidget(progressBar,2,0,1,2);
    mainLayout->addWidget(starBtn,3,1);
    mainLayout->setContentsMargins(10,10,10,10);
    mainLayout->setSpacing(10);
    connect (starBtn, &QPushButton::clicked, this, &ProgressDlg::startProgress);
}

ProgressDlg::~ProgressDlg()
{
}

void ProgressDlg::startProgress()
{
    bool ok;
    int num =FileNumLineEdit->text().toInt(&ok);
    if (comboBox->currentIndex () == 0) //采用进度条的方式显示进度
    {
        progressBar->setRange(0, num); //(b)
        for(int i=1;i<num+1;i++)
        {
            progressBar->setValue(i); // (c)
        }
    }

    else if (comboBox->currentIndex () ==1)
    {
        //创建一个进度对话框
        QProgressDialog *progressDialog=new QProgressDialog(this);
        QFont font("ZYSong18030",12);
        progressDialog->setFont(font);
        progressDialog->setWindowModality(Qt::WindowModal);
        /*minimumDuration 为进度条对话框出现前的
        等待时间。系统根据所需完成的工作量估算一个预计花费的时间，若大于设定的等待时间
        (minimumDuration), 则出现进度条对话框；若小于设定的等待时间 (minimumDuration), 则不
        出现进度条对话框。*/
        progressDialog->setMinimumDuration(5);
        progressDialog->setWindowTitle(tr("Please Wait"));
        progressDialog->setLabelText (tr("Copying ... "));
        progressDialog->setCancelButtonText (tr ("Cancel"));
        progressDialog->setRange(0,num); //设置进度对话框的步进范围
        for(int i=1;i<num+1;i++)
        {
            progressDialog->setValue(i);
            if(progressDialog->wasCanceled())
                return;
        }
    }

}

